<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>留言管理</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/semantic-ui/2.2.4/semantic.min.css">
  <link rel="stylesheet" href="../static/css/typo.css" th:href="@{/css/typo.css}">
  <link rel="stylesheet" href="../static/css/animate.css" th:href="@{/css/animate.css}">
  <link rel="stylesheet" href="../static/lib/prism/prism.css" th:href="@{/lib/prism/prism.css}">
  <link rel="stylesheet" href="../static/lib/tocbot/tocbot.css" th:href="@{/lib/tocbot/tocbot.css}">
  <link rel="stylesheet" href="../static/css/me.css" th:href="@{/css/me.css}">
</head>
<body>

  <!--导航-->
  <nav th:replace="admin/_fragments :: menu(4)" class="ui inverted attached segment m-padded-tb-mini m-shadow-small" >
  </nav>

  <!--中间内容-->
  <div  class="m-container-small m-padded-tb-big">
    <div class="ui container">
      <!--留言信息-->
      <div class="ui attached bottom segment">
        <div th:each="blog:${pageInfo.list}" >
          <div id="comment-container"  class="ui teal segment">
            <div th:fragment="commentList">
              <div class="ui threaded comments" style="max-width: 100%;">
                <h3 class="ui dividing header" th:text="${blog.title}"></h3>
                <div class="comment" th:each="comment : ${blog.getComments()}">
                  <a class="avatar">
                    <img src="https://unsplash.it/100/100?image=1005" th:src="@{${comment.avatar}}">
                  </a>
                  <div class="content">
                    <a class="author" >
                      <span th:text="${comment.nickname}">Matt</span>
                      <div class="ui mini basic teal left pointing label" th:if="${comment.nickname}=='Glorysun'">博主</div>
                      <!--<div class="ui mini basic teal left pointing label m-padded-mini" th:if="${comment.adminComment}">博主</div>-->
                    </a>
                    <div class="metadata">
                      <span class="date" th:text="${#dates.format(comment.createTime,'yyyy-MM-dd HH:mm')}">Today at 5:42PM</span>
                    </div>
                    <div class="text" th:text="${comment.content}">
                      How artistic!
                    </div>
                    <div class="actions">
                      <a class="reply" data-commentid="1" data-commentnickname="Matt" th:attr="data-commentid=${comment.id},data-commentnickname=${comment.nickname},data-blogid=${comment.blogId}" onclick="reply(this)">回复</a>
                    </div>
                  </div>

                  <!-- 层级评论 -->
                  <div class="comments" th:if="${#arrays.length(comment.replyComments)}>0">
                    <div class="comment" th:each="reply : ${comment.replyComments}">
                      <a class="avatar">
                        <img src="https://unsplash.it/100/100?image=1005" th:src="@{${reply.avatar}}">
                      </a>
                      <div class="content">
                        <a class="author" >
                          <span th:text="${reply.nickname}">Matt</span>
                          <div class="ui mini basic teal left pointing label" th:if="${reply.nickname}=='Glorysun'">博主</div>
                          &nbsp;
                          <span th:text="|@  ${reply.parentComment.nickname}|" class="m-teal">@ 小白</span>
                          <div class="ui mini basic teal left pointing label" th:if="${reply.parentComment.nickname}=='Glorysun'">博主</div>
                        </a>
                        <div class="metadata">
                          <span class="date" th:text="${#dates.format(reply.createTime,'yyyy-MM-dd HH:mm')}">Today at 5:42PM</span>
                        </div>
                        <div class="text" th:text="${reply.content}">
                          How artistic!
                        </div>
                        <div class="actions">
                          <a class="reply" data-commentid="1" data-commentnickname="Matt"
                             th:attr="data-commentid=${reply.id},data-commentnickname=${reply.nickname}
                             ,data-blogid=${reply.blogId}"
                             onclick="reply(this)">回复</a>
                        </div>
                      </div>

                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <br>
        </div>

        <!-- 回复 -->
        <form id="blog-form" th:action="@{/admin/commentReply}" method="post" class="ui form">
          <div id="comment-form" class="ui form">
            <input type="hidden" name="blogId" value="1"/>
            <input type="hidden" name="parentCommentId" value="-1">
            <div class="field">
              <textarea name="content" placeholder="请输入评论信息..."></textarea>
            </div>
            <div class="fields">
              <div class="field m-mobile-wide m-margin-bottom-small">
                <div class="ui left icon input">
                  <i class="user icon"></i>
                  <input type="text" name="nickname"  value="Glorysun">
                </div>
              </div>
              <div class="field m-mobile-wide m-margin-bottom-small">
                <div class="ui left icon input">
                  <i class="mail icon"></i>
                  <input type="text" name="email"  value="582366752@qq.com">
                </div>
              </div>
              <div class="field  m-margin-bottom-small m-mobile-wide">
                <button  type="submit" class="ui teal button m-mobile-wide"><i class="edit icon"></i>发布</button>
              </div>
            </div>

          </div>
        </form>

      </div>
      <tfoot>
      <tr>
        <!--th colspan是合并单元格-->
        <th colspan="6">
          <div class="ui menu pagination" >
            <div class="item"><a th:href="@{/admin/comments}">首页</a></div>
            <div class="item"><a th:href="@{/admin/comments(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}">上一页</a></div>
            <div class="item"><a th:href="@{/admin/comments(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}">下一页</a></div>
            <div class="item"><a th:href="@{/admin/comments(pageNum=${pageInfo.pages})}">尾页</a></div>
          </div>
          <div class="ui segment m-inline-block">
            <p >当前第<span th:text="${pageInfo.pageNum}"></span>页，总<span th:text="${pageInfo.pages}"></span>页，共<span th:text="${pageInfo.total}"></span>条记录</p>
          </div>

        </th>
      </tr>
      </tfoot>
    </div>
  </div>

  <br>
  <br>
  <!--底部footer-->
  <footer th:replace="admin/_fragments :: footer" class="ui inverted vertical segment m-padded-tb-massive">
  </footer>
  <!--/*/<th:block th:replace="admin/_fragments :: script">/*/-->
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2/dist/jquery.min.js"></script>
  <script src="https://cdn.jsdelivr.net/semantic-ui/2.2.4/semantic.min.js"></script>
  <script src="../../static/lib/editormd/editormd.min.js" th:src="@{/lib/editormd/editormd.min.js}"></script>
  <!--/*/</th:block>/*/-->
  <script>

    $('.menu.toggle').click(function () {
      $('.m-item').toggleClass('m-mobile-hide');
    });
    $("#reward").popup({
      popup : $(".pay"),
      on: 'click',
      position : 'bottom center'
    });

    tocbot.init({
      // Where to render the table of contents.
      tocSelector: '.js-toc',
      // Where to grab the headings to build the table of contents.
      contentSelector: '.js-toc-content',
      // Which headings to grab inside of the contentSelector element.
      headingSelector: 'h1, h2, h3',
    });

    $('.toc.button').popup({
      popup : $(".toc-container.popup"),
      on: 'click',
      position : 'left center'
    });


    //评论表单验证
    $('.ui.form').form({
      fields: {
        title: {
          identifier: 'content',
          rules: [{
            type: 'empty',
            prompt: '请输入评论内容'
          }
          ]
        },
        content: {
          identifier: 'nickname',
          rules: [{
            type: 'empty',
            prompt: '请输入你的昵称'
          }]
        },
        type: {
          identifier: 'email',
          rules: [{
            type: 'email',
            prompt: '请填写正确的邮箱地址'
          }]
        }
      }
    });



    //校验
    $('#commentpost-btn').click(function () {
      var boo = $('.ui.form').form('validate form');
      if (boo) {
        console.log('校验成功');
        postData();
      } else {
        console.log('校验失败');
      }
    });

    function postData(){
      $.ajax({
        url:"${pageContext.request.contextPath}/admin/commentReply",
        method:"post",
        data:{
          "blogId":$("#blogId").val(),
          "nickname":$("#nickname").val(),
          "email":$("#email").val(),
          "content":$("#content").val(),
          "parentCommentId":$("#parentCommentId").val()
        },
        success:function(response){
          $(window).scrollTo($('#comment-container'),500);
          clearContent();
        },
        error:function(){
          console.log("请求发送失败..")
        }
      });
    }

    function clearContent() {
      $("[name='content']").val('');
      $("[name='parentCommentId']").val(-1);
      $("[name='content']").attr("placeholder", "请输入评论信息...");
    }

    function reply(obj) {
      var commentId=$(obj).data('commentid');
      var commentNickname=$(obj).data('commentnickname');
      var blogId=$(obj).data('blogid');
      $("[name='content']").attr("placeholder","@"+commentNickname).focus();
      $("[name='parentCommentId']").val(commentId);
      $("[name='blogId']").val(blogId);
      $(window).scrollTo($('#comment-form'),500);
    }
  </script>
</body>
</html>
